<?php 
	class NotificationController
	{

		private $model;
		function __construct($model)
		{
			$this->model=$model;
		}
		function get_counter()
		{
			$user_id=$_POST['receiver_id'];
			$type=$_POST['notification_type'];
			$counter=$this->model->get_counter($user_id,$type);
			return $counter;
		}
		function like_clicked()
		{
			global $current_user;
			$actor_id=$current_user->id;//id cua nguoi da kich hoaat hoat dong
			$item_id=$_POST['image_id'];
			$notification_type=$_POST['notification_type'];
			$receiver_id=$this->model->get_image_author_id($item_id); //id cua nguoi dang anh
			$like_status=$_POST['like_status'];
			if($notification_type=='like')
			{
				if($like_status==0)
				{
					$this->model->insert_notification($actor_id,$receiver_id,$notification_type,$item_id);
					$ip=$this->model->get_user_ip($receiver_id);
					$user_data = array(
					        'receiver_id' => $receiver_id,
					     );

					echo json_encode($user_data);
					die();

				}
				else
				{
					$this->model->delete_notification($actor_id,$receiver_id,$notification_type,$item_id);
					die();
				}
				
			}
			else if($notification_type=='comment')
			{
				header('Content-type: application/json');
				echo json_encode(
				array(
				    "receiver_id" => $receiver_id
				    )
				);

			}

			
		}




		function save_notification($ip,$activate_user,$author,$type,$item_id)
		{
			$notification = $this->model->is_notification_exist($activate_user,$author,$notification_type,$item_id);
			if($notification)
			{
				$this->model->delete_notification($activate_user,$author,$notification_type,$item_id);
			}
		}

		function save_notification_draft($ip,$activate_user,$author,$type,$item_id)
		{
			$result;
			$notification = $this->model->get_notification($author,$type);
			if($notification)
			{
				$notification_id=$notification->id; //lay id cua notification duoc up date
				if($this->model->is_item_exist($notification_id,$item_id))
				{
					$this->model->delete_noification_item($notification_id,$item_id);//neu nhu da ton tai thi xoa khoi 
					$result=$this->model->update_notification_counter($author,$type,-1);//cap nhap lai so luong notification giam 1 

				}
				else 
				{
					$this->model->insert_noification_item($notification_id,$item_id,$activate_user);
					$result=$this->model->update_notification_counter($author,$type,1);//cap nhap lai so luong notification tang 1
				}
			}
			else
			{
				$result=$this->model->insert_notification('0',$author,$type); // them notification moi cho user moi
				$notification = $this->model->get_notification($author,$type);
				$notification_id=$notification->id; //lay id cua notification duoc up date
				$this->model->insert_noification_item($notification_id,$item_id,$activate_user);//danh cho user chua dang nhap lan nao,nhap them dong moi cho notification item voi notification id va id cua item nhu like id hoac comment id ...
				
			}
		}
	}

?>